package com.ruoyi.aitcommon.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import com.ruoyi.aitcommon.vo.FlowContextVO;
import cn.hutool.core.util.StrUtil;

/**
 * FLow工具类
 * 存储FLow上下文和运行时数据
 * @author Administrator
 *
 */
public class FlowUtils {
	private static ThreadLocal<Map<String, Object>> threadCache = new ThreadLocal<Map<String, Object>>();
//	flowContext中是一个强类型结构,存放flowid,flowname等信息
	private static final String FLOWCONTEXT="flowContext";
	
	/**
	 * 在线程缓存中创建并初始化flow上下文
	 * @param code
	 * @param name
	 * @throws Exception
	 */
	public static void setFlowContext(String code,String name) throws Exception {
		if(StrUtil.isBlank(code)) {
			throw new Exception("流程编码为空");
		}
		
		FlowContextVO flow=new FlowContextVO();
//		构造流程map
		Long flowid = IDGenUtils.getLongID();
		flow.setFlowID(flowid);
		flow.setFlowCode(code);
		flow.setFlowName(name);
		flow.setStartDate(new Date());
		setFlowContext(flow);
	}
	
	public static void setFlowContext(FlowContextVO flow) {
//		上下文中put流程map
		if (threadCache.get() == null)
			threadCache.set(new LinkedHashMap<String, Object>());
		threadCache.get().put(FLOWCONTEXT, flow);
	}

	public static void clearFlowContext() {
		if (threadCache.get() != null)
			threadCache.get().remove(FLOWCONTEXT);
	}
	

	public static FlowContextVO getFlowContext() {
		FlowContextVO flow = null;
		if (threadCache.get() != null) {
			flow = (FlowContextVO)threadCache.get().get(FLOWCONTEXT);
			if(flow!=null) {
//				设置结束时间/耗时
				flow.setEndDate(new Date());
				Long diff = flow.getEndDate().getTime() - flow.getStartDate().getTime();// 毫秒
				flow.setTimeConsume(diff);
			}
		}
		return flow;
	}
	
	public static Long getFlowID() {
		FlowContextVO flow = getFlowContext();
		if (flow == null)
			return 0l;
		return flow.getFlowID();
	}

	public static String getFlowCode() {
		FlowContextVO flow = getFlowContext();
		if (flow == null)
			return "";
		return flow.getFlowCode();
	}
	
	public static void setFlowCode(String code) {
		FlowContextVO flow = getFlowContext();
		if (flow == null)
			return;
		flow.setFlowCode(code);
	}
	
	public static String getFlowName() {
		FlowContextVO flow = getFlowContext();
		if (flow == null)
			return "";
		return flow.getFlowName();
	}
	
	public static void setFlowName(String name) {
		FlowContextVO flow = getFlowContext();
		if (flow == null)
			return;
		flow.setFlowName(name);
	}
	
	public static void setRetCode(String code) {
		FlowContextVO flow = getFlowContext();
		if (flow == null)
			return;
		flow.setRetCode(code);
	}
	
	public static String getRetCode() {
		FlowContextVO flow = getFlowContext();
		if (flow == null)
			return "";
		return flow.getRetCode();
	}
	
	public static Date getStartDate() {
		FlowContextVO flow = getFlowContext();
		if(flow==null) return null;
		return flow.getStartDate();
	}
	
	public static Long getTimeConsume() {
		FlowContextVO flow = getFlowContext();
		if(flow==null) return 0l;
		return flow.getTimeConsume();
	}
	
	public static void addSuccessCnt() {
		FlowContextVO flow = getFlowContext();
		if(flow==null) return;
		
		Long cnt = flow.getSuccessCnt()+1;
		flow.setSuccessCnt(cnt);

	}
	
	public static Long getSuccessCnt() {
		FlowContextVO flow = getFlowContext();
		if(flow==null) return 0l;
		
		return flow.getSuccessCnt();
	}
	
	public static void addErrorCnt() {
		FlowContextVO flow = getFlowContext();
		if(flow==null) return;
		
		Long cnt=flow.getErrorCnt()+1;
		flow.setErrorCnt(cnt);
	}
	
	public static Long getErrorCnt() {
		FlowContextVO flow = getFlowContext();
		if(flow==null) return 0l;
		
		return flow.getErrorCnt();
	}
	
	public static void addBulletin(String msg) {
		FlowContextVO flow=getFlowContext();
		if(flow==null) return;
		Date date=new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String msg1="["+sdf.format(date)+"]"+msg;
//		flow.getBulletin().add(msg1);
		flow.addBulletin(msg1);
	}
	
	public static void addBulletin(String msg,Object... args) {
//		String msg1=String.format(msg, args);
		String msg1=StrUtil.format(msg, args);
		addBulletin(msg1);
	}
	
	public static String getBulletinStr() {
		FlowContextVO flow=getFlowContext();
		if(flow==null) return "";
		
		return flow.getBulletinStr();
	}
}
